package com.hualan.dao.impl;

import com.hualan.bean.PanghuShop;
import com.hualan.dao.MenDianDao;
import com.hualan.utils.DBUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MenDianDaoImpl implements MenDianDao {
    @Override
    /*
    根据分页信息查看门店信息
     */
    public List<PanghuShop> queryMenDianWithPage(int page, int size) {
        List<PanghuShop> shopList = DBUtils.query("select * from panghu_shop limit ?,?", PanghuShop.class, (page - 1) * size, size);
        return shopList;
    }

    @Override
    /*
    根据前端传过来的信息添加门店
     */
    public int addMenDian(PanghuShop shop) {
        int row = DBUtils.update("insert into panghu_shop(shop_name,shop_describe,shop_image,shop_phone,shop_province,shop_city,shop_district,shop_address,shop_longitude,shop_latitude) values(?,?,?,?,?,?,?,?,?,?)", shop.getShopName(), shop.getShopDescribe(), shop.getAddTime(), shop.getShopPhone(), shop.getShopProvince(), shop.getShopCity(), shop.getShopDistrict(), shop.getShopAddress(), shop.getShopLongitude(), shop.getShopLatitude());
        return row;
    }

    @Override
    /*
    根据前端传过来的信息修改门店
     */
    public int updateMenDian(PanghuShop shop) {
        int row = DBUtils.update("update panghu_shop set shop_name = ?,shop_describe = ?,shop_image = ?,shop_phone = ?,shop_province = ?,shop_city = ?,shop_district = ?,shop_address = ?,shop_longitude = ?,shop_latitude = ? where shop_id = ?", shop.getShopName(), shop.getShopDescribe(), shop.getAddTime(), shop.getShopPhone(), shop.getShopProvince(), shop.getShopCity(), shop.getShopDistrict(), shop.getShopAddress(), shop.getShopLongitude(), shop.getShopLatitude(), shop.getShopId());
        return row;
    }

    @Override
    /*
    查看页面的总条数
     */
    public Long selectMenDinCount() {
        Map<String, Object> map = DBUtils.queryOne("select count(shop_id) count from panghu_shop");
        Long count = (Long) map.get("count");
        System.out.println(count);
        return count;
    }


    @Override
    /*
    根据条件进行查询
     */
    public List<PanghuShop> queryCondition(String name, String phone, String startTime, String endTime, Integer pageSize, Integer pageNum) {
        System.out.println(pageNum + "hhhhh");
        System.out.println(pageSize + "xxxx");
        //存放参数的list集合
        ArrayList<Object> parameterList = new ArrayList<>();
        //初始条件
        String conditionParameter = " where 1 = 1 ";
        if (name != null && !name.equals("")) {
            conditionParameter = conditionParameter + " and shop_name = ?";
            parameterList.add(name);
        }
        if (phone != null && !phone.equals("")) {
            conditionParameter = conditionParameter + " and shop_phone = ?";
            parameterList.add(phone);
        }
        if ((startTime != null && !startTime.equals("")) && (endTime != null && !endTime.equals(""))) {
            conditionParameter = conditionParameter + " and add_time between ? and ? ";
            parameterList.add(startTime);
            parameterList.add(endTime);
        }
        //拼接上当前页和每页显示的数据
//        System.out.println(11+pageNum);
//        System.out.println(22+pageSize);
        conditionParameter = conditionParameter + " limit " + pageNum + "," + pageSize;
        String sql = "select * from panghu_shop" + conditionParameter;
//        System.out.println(sql);
        //将查询的数据进行返回
        return DBUtils.query(sql, PanghuShop.class, parameterList.toArray(new Object[0]));
    }

}
